//
//  SecondViewController.m
//  PickerManager
//
//  Created by iOS开发 on 2018/8/14.
//  Copyright © 2018年 iOS开发. All rights reserved.
//

#import "SecondViewController.h"
#import "ImagePickerView.h"
#import "ImagePickerManager.h"
#import "PhotoBrowserVC.h"
#import "PhotoBrowserAnimator.h"
#import "UIImageViewLocationModel.h"

@interface SecondViewController ()<ImagePickerViewDelegate>

@property (nonatomic, strong) PhotoBrowserAnimator *animator;
@property (nonatomic, strong) ImagePickerView *pickerView;
@property (nonatomic, strong) ImagePickerManager *pickerManager;

@end

@implementation SecondViewController

- (void)dealloc {
    NSLog(@"释放");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 选择图片管理器
    _pickerManager = [[ImagePickerManager alloc] init];
    // 转场动画
    _animator = [[PhotoBrowserAnimator alloc] init];
    // 图片视图
    _pickerView = [[ImagePickerView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 0) AndHorizontalCount:3];
    _pickerView.delegate = self;
    [self.view addSubview:_pickerView];
    
    // 配置
    _pickerManager.isShowSheet = YES;
    _pickerView.maxCount = 6;
    _pickerManager.maxCount = 6;
    
    
}

#pragma mark - pickerViewDelegate
// 添加图片
- (void)pushToPicker {
    __weak typeof(self)WeakSelf = self;
    // 相册选择返回图片
    [_pickerManager pushPickerFromVC:self block:^(NSArray *imageArr) {
        [WeakSelf.pickerView updatePhotos:imageArr];
    }];
    // 拍摄返回图片
    _pickerManager.cameraBlock = ^(UIImage *image) {
        [WeakSelf.pickerView addCameraImage:image];
    };
}

// 查看图片
- (void)checkImageWithIndex:(NSIndexPath *)index withPhotos:(NSMutableArray *)photos {
    PhotoBrowserVC *photoVC = [[PhotoBrowserVC alloc] initWithPhotos:photos andIndex:index.item animator:_animator];
    photoVC.transitioningDelegate = self.animator;
    self.animator.startIndex = index.item;
    self.animator.endIndex = index.item;
    self.animator.photosModelArr = [self createModel:photos];
    
    [self presentViewController:photoVC animated:YES completion:nil];
    
    // 删除
    __weak typeof(self)WeakSelf = self;
    photoVC.block = ^(NSInteger indexItem) {
        [photos removeObjectAtIndex:indexItem];
        [WeakSelf.pickerView updatePhotos:photos];
        [WeakSelf.pickerManager deleteSelectPhoto:indexItem];
    };
}

// 整理数组
- (NSMutableArray *)createModel:(NSMutableArray *)photos {
    NSMutableArray *modelArr = @[].mutableCopy;
    [photos enumerateObjectsUsingBlock:^(UIImage *  _Nonnull image, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImageViewLocationModel *model = [[UIImageViewLocationModel alloc] init];
        model.cellImage = image;
        model.index = idx;
        model.imageViewFrame = [self.pickerView getCellPositionWithIndex:[NSIndexPath indexPathForRow:idx inSection:0] inView:self.view];
        [modelArr addObject:model];
    }];

    return modelArr;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
